
package logic.actions;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;

import controller.SEISaudeDefaultFacade;

public class SEISaudeAction extends Action {
	private Map<String,String> formActionParams;
	private boolean isUserChecked;
	
	public SEISaudeAction() {
		super();
		setFormActionParams(new HashMap<String,String>());
		setUserChecked(true);
	}
	
	public void setFormActionParams(Map<String,String> formActionParams) {
		this.formActionParams = formActionParams;
	}
	
	public String addFormActionParam(String key,String value) {
		return getFormActionParams().put(key,value);
	}
	
	public String removeFormActionParam(String key) {
		return getFormActionParams().remove(key);
	}
	
	public Map<String,String> getFormActionParams() {
		return formActionParams;
	}
	
	public void setUserChecked(boolean isUserChecked) {
		this.isUserChecked = isUserChecked;
	}
	
	public boolean isUserChecked() {
		return isUserChecked;
	}
	
	protected void setUpRequest(SEISaudeDefaultFacade<?,Long> facade,ActionForm form,HttpServletRequest request) {
		request.setAttribute("backPage","home.do");
		request.setAttribute("homePage","home.do");
		request.setAttribute("formActionParams",getFormActionParamsString());
	}
	
	private String getFormActionParamsString() {
		String result = formActionParams.isEmpty() ? "" : "?";
		Iterator<String> keysIt = formActionParams.keySet().iterator();
		while (keysIt.hasNext()) {
			String eachKey = keysIt.next();
			result += eachKey + "=" + formActionParams.get(eachKey);
			if (keysIt.hasNext()) result += "&";
		}
		
		return result;
	}
	
	public boolean checkUserLogged(HttpServletRequest request) {
		return request.getSession() != null && request.getSession().getAttribute("userName") != null;
	}
}
